package com.xbongbong.saas.help;

import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.CreditFixedLimitService;
import com.xbongbong.saas.service.CreditTemporaryLimitService;
import com.xbongbong.saas.service.DistributorCommunicateService;
import com.xbongbong.saas.service.DistributorContactService;
import com.xbongbong.saas.service.DistributorManageService;
import com.xbongbong.saas.service.OrderOutstockService;
import com.xbongbong.saas.service.OrderService;
import com.xbongbong.saas.service.ReturnService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 经销商专用
 * @author xinpeng.jiang
 * @date 2020/12/22 15:54
 * @version 1.0
 * @since V1.0
 */
@Component
public class DistributorAddHelp {

    private static final Logger LOG = LoggerFactory.getLogger(DistributorAddHelp.class);

    @Resource
    private DistributorManageService distributorManageService;
    @Resource
    private DistributorContactService distributorContactService;
    @Resource
    private OrderService orderService;
    @Resource
    private DistributorCommunicateService distributorCommunicateService;
    @Resource
    private CreditTemporaryLimitService creditTemporaryLimitService;
    @Resource
    private CreditFixedLimitService creditFixedLimitService;
    @Resource
    private ReturnService returnService;
    @Resource
    private OrderOutstockService orderOutstockService;
    @Resource
    private SaasAddHelp saasAddHelp;
    @Resource
    private DefaultAttrHelp defaultAttrHelp;

    /**
     * saas新建时给解释字段赋初始值,格式化
     *
     * @param formatExplainDTO 表单解释格式化入参
     * @throws XbbException 抛出业务异常
     * @author xinpeng.jiang
     */
    public void formatExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        Integer businessType = formatExplainDTO.getBusinessType();
        if (businessType == null || businessType == 0) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
        }
        RedundantTemplateTypeEnum redundantTemplateTypeEnum = RedundantTemplateTypeEnum.getByCode(businessType);
        boolean needSource = true;
        switch (redundantTemplateTypeEnum) {
            case CUSTOMER:
                distributorManageService.formatAddExplain(formatExplainDTO);
                break;
            case CONTACT:
                distributorContactService.formatAddExplain(formatExplainDTO);
                break;
            case CONTRACT:
                orderService.formatAddExplain(formatExplainDTO);
                break;
            case COMMUNICATE:
                distributorCommunicateService.formatAddExplain(formatExplainDTO);
                break;
            case REFUND:
                returnService.formatAddExplain(formatExplainDTO);
                break;
            case CREDIT_TEMPORARY_LIMIT:
                creditTemporaryLimitService.formatAddExplain(formatExplainDTO);
                break;
            case CREDIT_FIXED_LIMIT:
                creditFixedLimitService.formatAddExplain(formatExplainDTO);
                break;
            case CONTRACT_OUTSTOCK:
                needSource = orderOutstockService.formatAddExplain(formatExplainDTO);
                break;
            default:
                saasAddHelp.formatExplain(formatExplainDTO);
                break;
        }
        if (needSource){
            for (FieldAttrEntity item : formatExplainDTO.getExplainList()) {
                if (Objects.equals(item.getFieldType(), FieldTypeEnum.SELECT_PRODUCT.getType())) {
                    List<FieldAttrEntity> subFormItemArray = (List<FieldAttrEntity>) item.getSubForm().getItems();
                    int linkProductItemIndex = 0;
                    for (int i = 0; i < subFormItemArray.size(); i++) {
                        FieldAttrEntity itemObj = subFormItemArray.get(i);
                        if (Objects.equals(itemObj.getAttr(), "text_1")) {
                            linkProductItemIndex = i;
                            break;
                        }
                    }
                    FieldAttrEntity fieldAttrEntity = subFormItemArray.get(linkProductItemIndex);
                    List<LinkedAttrPoJo> linkedAttrPoJos = fieldAttrEntity.getLinkInfo().getLinkedAttr();
                    formatExplainDTO.setProductField(item);
                    defaultAttrHelp.addProductLinkAttr(XbbRefTypeEnum.getByCode(businessType), linkedAttrPoJos, formatExplainDTO);
                    subFormItemArray.set(linkProductItemIndex, fieldAttrEntity);
                    item.getSubForm().setItems(subFormItemArray);
                }
            }
        }
    }
}
